package com.lanou.wuyou.gift.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.HomeBean;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/15.
 */

public class HomeListAdapter extends BaseAdapter{
    private Context context;
    private HomeBean homeBean;

    public void setHomeBean(HomeBean homeBean) {
        this.homeBean = homeBean;
        notifyDataSetChanged();
    }

    public HomeListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        //防止数据中个别数据字段为空,尽可能多级判断
        if (homeBean == null){
            return 0;
        }
        if (homeBean.getData() == null){
            return 0;
        }
        return homeBean.getData().getItems() != null && homeBean.getData().getItems().size() > 0 ? homeBean.getData().getItems().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return homeBean.getData().getItems().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_home_list,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (homeBean != null){
            HomeBean.DataBean.ItemsBean itemsBean = homeBean.getData().getItems().get(position);
            if (itemsBean != null){
                Picasso.with(context).load(itemsBean.getCover_image_url()).into(viewHolder.bannerIv);
                viewHolder.titleCTv.setText(itemsBean.getTitle());
            }
            HomeBean.DataBean.ItemsBean.AuthorBean authorBean = homeBean.getData().getItems().get(position).getAuthor();
            if (authorBean != null){
                //进行图片网络请求
                Picasso.with(context).load(authorBean.getAvatar_url()).into(viewHolder.avatarIv);
                viewHolder.nicknameTv.setText(authorBean.getNickname());
                viewHolder.introductionTv.setText(authorBean.getIntroduction());
            }
            String introduction = homeBean.getData().getItems().get(position).getIntroduction();
            if (introduction != null){
                viewHolder.introductionCTv.setText(introduction);
            }
           String likes = String.valueOf(homeBean.getData().getItems().get(position).getLikes_count());
            if (likes != null){
                viewHolder.likesTv.setText(likes);
            }
            String title = homeBean.getData().getItems().get(position).getTitle();
            if(title != null){
                viewHolder.titleTv.setText(title);
            }
        }
        return convertView;
    }
    class ViewHolder{
        private TextView nicknameTv,introductionTv,titleTv,introductionCTv,columnTv,titleCTv,likesTv;
        private ImageView avatarIv,bannerIv,likesIv;
        public ViewHolder(View itemView){
            nicknameTv = (TextView) itemView.findViewById(R.id.nickname_tv);
            introductionTv = (TextView) itemView.findViewById(R.id.introduction_tv);
            titleTv = (TextView) itemView.findViewById(R.id.title_tv);
            introductionCTv = (TextView) itemView.findViewById(R.id.introduction_content);
            columnTv = (TextView) itemView.findViewById(R.id.column_tv);
            titleCTv = (TextView) itemView.findViewById(R.id.title_column);
            likesTv = (TextView) itemView.findViewById(R.id.likes_tv);
            avatarIv = (ImageView) itemView.findViewById(R.id.avatar_iv);
            bannerIv = (ImageView) itemView.findViewById(R.id.banner_iv);
            likesIv = (ImageView) itemView.findViewById(R.id.likes_iv);
        }
    }
}
